﻿using Microsoft.AspNetCore.Mvc;

namespace PageOffice6_Net_Core.Controllers.ExcelTable
{
    public class ExcelTableController : Controller
    {
        public IActionResult Excel()
        {
            PageOfficeNetCore.PageOfficeCtrl poCtrl = new PageOfficeNetCore.PageOfficeCtrl(Request);

            //定义Workbook对象
            PageOfficeNetCore.Excel.WorkbookWriter workBook = new PageOfficeNetCore.Excel.WorkbookWriter();
            //定义Sheet对象，"Sheet1"是打开的Excel表单的名称
            PageOfficeNetCore.Excel.SheetWriter sheet = workBook.OpenSheet("Sheet1");
            //定义Table对象
            PageOfficeNetCore.Excel.ExcelTableWriter table = sheet.OpenTable("B4:F13");
            for (int i = 0; i < 50; i++)
            {
                table.DataFields[0].Value = "产品 " + i.ToString();
                table.DataFields[1].Value = "100";
                table.DataFields[2].Value = (100 + i).ToString();
                table.NextRow();
            }
            table.Close();

            poCtrl.SetWriter(workBook);// 注意不要忘记此代码，如果缺少此句代码，不会赋值成功。

            //打开Excel文档
            poCtrl.WebOpen("doc/test.xls", PageOfficeNetCore.OpenModeType.xlsNormalEdit, "tom");
            ViewBag.poCtrl = poCtrl.GetHtml();
            return View();
        }
    }
}